package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.bo.ZjExperimentBindingBo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMarshallRequiresBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMarshallRequiresVo;
import com.ruoyi.zhgd.domain.NLaboratoryMarshallRequires;
import com.ruoyi.zhgd.mapper.NLaboratoryMarshallRequiresMapper;
import com.ruoyi.zhgd.service.INLaboratoryMarshallRequiresService;

import java.util.*;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2023-10-13
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMarshallRequiresServiceImpl implements INLaboratoryMarshallRequiresService {

    private final NLaboratoryMarshallRequiresMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public NLaboratoryMarshallRequiresVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<NLaboratoryMarshallRequiresVo> queryPageList(NLaboratoryMarshallRequiresBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMarshallRequires> lqw = buildQueryWrapper(bo);
        lqw.eq(NLaboratoryMarshallRequires::getDeleted,0);
        Page<NLaboratoryMarshallRequiresVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<NLaboratoryMarshallRequiresVo> queryList(NLaboratoryMarshallRequiresBo bo) {
        LambdaQueryWrapper<NLaboratoryMarshallRequires> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMarshallRequires> buildQueryWrapper(NLaboratoryMarshallRequiresBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMarshallRequires> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryMarshallRequires::getBaseCode, bo.getBaseCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), NLaboratoryMarshallRequires::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getInsertBy()), NLaboratoryMarshallRequires::getInsertBy, bo.getInsertBy());
        lqw.ge(bo.getInsertTime() != null, NLaboratoryMarshallRequires::getInsertTime, bo.getInsertTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleted()), NLaboratoryMarshallRequires::getDeleted, bo.getDeleted());
        lqw.eq(bo.getFlowMinValue() != null, NLaboratoryMarshallRequires::getFlowMinValue, bo.getFlowMinValue());
        lqw.eq(bo.getFlowMaxValue() != null, NLaboratoryMarshallRequires::getFlowMaxValue, bo.getFlowMaxValue());
        lqw.eq(bo.getStabilityValue() != null, NLaboratoryMarshallRequires::getStabilityValue, bo.getStabilityValue());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), NLaboratoryMarshallRequires::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NLaboratoryMarshallRequires::getDeptPkid, bo.getDeptPkid());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(NLaboratoryMarshallRequires bo) {
        bo.setInsertTime(DateUtils.getNowDate());
        NLaboratoryMarshallRequires add = BeanUtil.toBean(bo, NLaboratoryMarshallRequires.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(NLaboratoryMarshallRequires bo) {

        NLaboratoryMarshallRequires update = BeanUtil.toBean(bo, NLaboratoryMarshallRequires.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMarshallRequires entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据条件获取指定马歇尔技术要求列表
     *
     * @param bo
     * @return
     */
    @Override
    public NLaboratoryMarshallRequires getMarshallRequires(ZjExperimentBindingBo bo) {
        return baseMapper.getMarshallRequires(bo);
    }

}
